/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Windows;

namespace Oceanside
{
    /// <summary>
    /// Each screen or pos window is actually a tabpanel that has now tab.  As the user moves
    /// through the application, each tabpanel is simply selected.
    /// </summary>
    public class PosWindow : System.Windows.Controls.Primitives.TabPanel
    {
        public int MyTabIndex = 0;

        private static PosWindow windowBeforeLock = null;
        public static PosWindow WindowBeforeLock
        {
            get { return PosWindow.windowBeforeLock; }
            set { PosWindow.windowBeforeLock = value; }
        }

        /// <summary>
        /// Each of these lists you will find below is a sort of dependency
        /// injection design.  During bootup these lists are filled with
        /// dependencies.  
        /// </summary>
        private static List<BindInterface> tsBoundFunctions = new List<BindInterface>();
        public static List<BindInterface> TsListBoundFunctions
        {
            get { return PosWindow.tsBoundFunctions; }
            set { PosWindow.tsBoundFunctions = value; }
        }

        private static List<BindInterface> cpBoundFunctions = new List<BindInterface>();
        public static List<BindInterface> CpListBoundFunctions
        {
            get { return PosWindow.cpBoundFunctions; }
            set { PosWindow.cpBoundFunctions = value; }
        }

        private static List<BindInterface> poBoundFunctions = new List<BindInterface>();
        public static List<BindInterface> PoListBoundFunctions
        {
            get { return PosWindow.poBoundFunctions; }
            set { PosWindow.poBoundFunctions = value; }
        }

        private static List<BindInterface> ccBoundFunctions = new List<BindInterface>();
        public static List<BindInterface> CCListBoundFunctions
        {
            get { return PosWindow.ccBoundFunctions; }
            set { PosWindow.ccBoundFunctions = value; }
        }

        private static List<BindInterface> finBoundFunctions = new List<BindInterface>();
        public static List<BindInterface> FinListBoundFunctions
        {
            get { return PosWindow.finBoundFunctions; }
            set { PosWindow.finBoundFunctions = value; }
        }

        private static List<BindInterface> reportBoundFunctions = new List<BindInterface>();
        public static List<BindInterface> ReportListBoundFunctions
        {
            get { return PosWindow.reportBoundFunctions; }
            set { PosWindow.reportBoundFunctions = value; }
        }

        private static List<BindInterface> splitBoundFunctions = new List<BindInterface>();
        public static List<BindInterface> SplitBoundFunctions
        {
            get { return PosWindow.splitBoundFunctions; }
            set { PosWindow.splitBoundFunctions = value; }
        }

        private static List<BindInterface> transferTableBoundFunctions = new List<BindInterface>();
        public static List<BindInterface> TransferTableBoundFunctions
        {
            get { return PosWindow.transferTableBoundFunctions; }
            set { PosWindow.transferTableBoundFunctions = value; }
        }

        private static List<BindInterface> combineTableBoundFunctions = new List<BindInterface>();
        public static List<BindInterface> CombineTableBoundFunctions
        {
            get { return PosWindow.combineTableBoundFunctions; }
            set { PosWindow.combineTableBoundFunctions = value; }
        }

        private static List<BindInterface> checkHistoryBoundFunctions = new List<BindInterface>();
        public static List<BindInterface> CheckHistoryBoundFunctions
        {
            get { return PosWindow.checkHistoryBoundFunctions; }
            set { PosWindow.checkHistoryBoundFunctions = value; }
        }

        private static Window mainPosWindow = null;
        public static Window MainPosWindow
        {
            get { return mainPosWindow; }
            set { mainPosWindow = value; }
        }

        private static PosWindow activeWindowInstance;
        public static PosWindow ActiveWindowInstance
        {
            get { return PosWindow.activeWindowInstance; }
            set { PosWindow.activeWindowInstance = value; }
        }

        private static PosWindow previousWindowInstance;
        public static PosWindow PreviousWindowInstance
        {
            get { return PosWindow.previousWindowInstance; }
            set { PosWindow.previousWindowInstance = value; }
        }

        /// <summary>
        /// This is true if a manager steps in to approve an action that an employee
        /// is not approved to do.  When the card is swiped, hidden buttons will appear.
        /// </summary>
        private static bool managerCardWasSwiped = false;
        public static bool ManagerCardWasSwiped
        {
            get { return PosWindow.managerCardWasSwiped; }
            set { PosWindow.managerCardWasSwiped = value; }
        }

        /// <summary>
        /// The ID of the manager that swiped.
        /// </summary>
        private static Int64 managerIdThatSwiped = 0;
        public static Int64 ManagerIdThatSwiped
        {
            get { return PosWindow.managerIdThatSwiped; }
            set { PosWindow.managerIdThatSwiped = value; }
        }

        /// <summary>
        /// This function is called to assure that the dependency injected bindings are visible or
        /// hidden based upon countless program state variables at the time refresh was called.
        /// </summary>
        /// <param name="type"></param>
        public static void RefreshVisibility(PROP_TYPE_ENUM type)
        {
            List<BindInterface> bindList;

            switch (type)
            {
                case PROP_TYPE_ENUM.TS:
                    bindList = TsListBoundFunctions;
                    break;
                case PROP_TYPE_ENUM.CP:
                    bindList = CpListBoundFunctions;
                    break;
                case PROP_TYPE_ENUM.PO:
                    bindList = PoListBoundFunctions;
                    break;
                case PROP_TYPE_ENUM.CC:
                    bindList = CCListBoundFunctions;
                    break;
                case PROP_TYPE_ENUM.FIN:
                    bindList = FinListBoundFunctions;
                    break;
                case PROP_TYPE_ENUM.SPL:
                    bindList = SplitBoundFunctions;
                    break;
                case PROP_TYPE_ENUM.RPT:
                    bindList = ReportListBoundFunctions; 
                    break;
                case PROP_TYPE_ENUM.XFR:
                    bindList = TransferTableBoundFunctions;
                    break;
                case PROP_TYPE_ENUM.COM:
                    bindList = CombineTableBoundFunctions;
                    break;
                case PROP_TYPE_ENUM.OH:
                    bindList = CheckHistoryBoundFunctions;
                    break;
                default:
                    bindList = TsListBoundFunctions;
                    break;
            }
            foreach (BindInterface bi in bindList)
            {
                bi.OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("IsVisible"));
            }
        }

        /// <summary>
        /// This is the type of refresh that should be performed.
        /// </summary>
        public enum PROP_TYPE_ENUM
        {
            TS,
            CP,
            PO,
            CC,
            FIN,
            SPL,
            RPT,
            XFR,
            OH,
            COM,
        }

        /// <summary>
        /// This lock will assure that the user or test app does not activate the process of switching to the next screen
        /// until the current screen has been fully switched to.  I am not sure if this is truly needed anymore, but 
        /// I would be careful removing it.
        /// </summary>
        public static object ScreenSwitchLock = new object();

        /// <summary>
        /// This variable is important.  We pop modal dialogs all over the application.  There is a chance where
        /// two of them could be popped up.  When that happens, the other one falls behind the main application 
        /// window and the main application never gets focus again.  Bad deal.  We need to assure that we
        /// never pop a modal dialog when there is already one open.
        /// </summary>
        public static Boolean ModalDialogIsOpen = false;

        /// <summary>
        /// 
        /// </summary>
        public PosWindow()
        {

        }

        /// <summary>
        /// Each POS Window (or actually tabpage) inherits this function and the function PrepareForScreenLeave.
        /// They are called before entering the tabpage or leaving the page to assure that the state of the 
        /// application is ready for user input.
        /// </summary>
        public virtual void PrepareForScreenChange()
        {

        }

        /// <summary>
        /// See above.
        /// </summary>
        public virtual void PrepareForScreenLeave()
        {
        }


        public delegate void SwitchWindowDelegateType(PosWindow window);
        public static SwitchWindowDelegateType SwitchWindowDelegate_NOTHROW;

    }
}


